package com.example.controller;

import com.example.bean.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ━━━━━━神兽出没━━━━━━
 * 　　 ┏┓     ┏┓
 * 　　┏┛┻━━━━━┛┻┓
 * 　　┃　　　　　┃
 * 　　┃　　━　　 ┃
 * 　　┃　┳┛　┗┳  ┃
 * 　　┃　　　　　 ┃
 * 　　┃　　┻　　　┃
 * 　　┃　　　　　 ┃
 * 　　┗━┓　　　┏━┛　Code is far away from bug with the animal protecting
 * 　　　 ┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　┣┓
 * 　　　　┃　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　 ┃┫┫ ┃┫┫
 * 　　　　 ┗┻┛ ┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(path = "/login", method = RequestMethod.POST)
    @ResponseBody
    public String login(HttpSession session, @RequestBody User user) {
        System.out.println(user);
        User res = userService.login(user);
        if(res != null){
            if(res.getStatus() != 1) return "no-active"; //没有激活的用户不能登录
            session.setAttribute("user", res);
            return "success";
        }
        return "error";
    }

    @RequestMapping(path = "/usernameCheck", method = RequestMethod.POST)
    @ResponseBody
    public String usernameCheck(String username) {
        return userService.selectByUsername(username) == null ? "success" : "error";
    }

    @RequestMapping(path = "/emailCheck", method = RequestMethod.POST)
    @ResponseBody
    public String emailCheck(String email) {
        return userService.selectByEmail(email) == null ? "success" : "error";
    }

    @RequestMapping(path = "/passwordCheck", method = RequestMethod.POST)
    @ResponseBody
    public String passwordCheck(int id, String password) {
        return userService.passwordCheck(id, password) ? "success" : "error";
    }

    @RequestMapping(path = "/register", method = RequestMethod.POST)
    @ResponseBody
    public String register(@RequestBody User user) {
        userService.register(user);
        return "success";

//        if() {
//            map.put("icon", "success");
//            map.put("title", "注册成功,我们已经向您的邮箱发送了一封激活邮件，请尽快激活!");
//        } else {
//            map.put("icon", "error");
//            map.put("title", "注册失败,请稍后重试!");
//        }
//        return map;
    }

    @RequestMapping(path = "/activation/{id}/{activationCode}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, String> activation(@PathVariable("id") int id, @PathVariable("activationCode")String activationCode) {
        Map<String, String> map = new HashMap<>();
        int activation = userService.activation(id, activationCode);
        if(activation == 0) {
            map.put("icon", "success");
            map.put("title", "您的账号已经激活成功,可以正常使用了!");
        } else if(activation == 1) {
            map.put("icon", "warning");
            map.put("title", "请勿重复激活");
        } else if(activation == -1) {
            map.put("icon", "error");
            map.put("title", "链接不存在");
        }
        return map;
    }

    @RequestMapping(path = "/getUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public User getUserInfo(HttpSession session) {
        return (User)session.getAttribute("user");
    }

    @RequestMapping(path = "/exit", method = RequestMethod.GET)
    @ResponseBody
    public void exit(HttpSession session) {
        session.removeAttribute("user");
    }

    @RequestMapping(path = "/updatePassword", method = RequestMethod.POST)
    @ResponseBody
    public void updatePassword(int id, String password) {
        userService.updatePassword(id, password);
    }

    @RequestMapping(path = "/search", method = RequestMethod.GET)
    @ResponseBody
    public List<User> search(String username, String email, int roleId, int status) {
        return userService.search(username, email, roleId, status);
    }

    @RequestMapping(path = "/updateUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public String updateUserInfo(@RequestBody User user) {
        userService.updateUserInfo(user);
        return "success";
    }

    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(int id) {
        return userService.delete(id) ? "success" : "error";
    }
}
